package de.baumann.browser.activitys.center

import android.content.Context
import android.content.Intent
import android.graphics.*
import android.support.design.widget.BottomSheetDialog
import android.widget.TextView
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import de.baumann.browser.R
import de.baumann.browser.adapter.ProPlanAdapter
import de.baumann.browser.api.net.vo.Rank
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.IProPlanView
import de.baumann.browser.present.ProPlanPresenter
import de.baumann.browser.utils.QrCodeUtil
import de.baumann.browser.utils.StringUtil
import de.baumann.browser.utils.getInviteCode
import de.baumann.browser.utils.getShareContent
import kotlinx.android.synthetic.main.activity_pro_plan.*
import kotlinx.android.synthetic.main.include_tool_bar.*


class ProPlanActivity : BaseOdinActivity<IProPlanView, ProPlanPresenter>(), IProPlanView {

    companion object {
        fun startProPlanActivity(context: Context) {
            context.startActivity(Intent(context, ProPlanActivity::class.java))
        }
    }

    override fun getTitleText(): String {
        return "矿工推广计划"
    }

    override fun getLayout(): Int {
        return R.layout.activity_pro_plan
    }

    private lateinit var proPlanAdapter: ProPlanAdapter

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        btnPlanInv.setOnClickListener {
            displaySharePanel()
        }
        tvInviteCode.text = getInviteCode()
        proPlanAdapter = ProPlanAdapter()
        rankList.adapter = proPlanAdapter
    }

    override fun initData() {
        presenter?.getRank()
    }

    private fun displaySharePanel() {
        val sheetDialog = BottomSheetDialog(mContext)
        sheetDialog.setContentView(R.layout.dialog_share)
        sheetDialog.findViewById<TextView>(R.id.copyLink)?.setOnClickListener {
            StringUtil.copy(mContext, "http://download.odinlink.club")
            showShortToast("已复制")
            sheetDialog.dismiss()
        }
        sheetDialog.findViewById<TextView>(R.id.shareToWechart)?.setOnClickListener {
            share(SHARE_MEDIA.WEIXIN)
        }

        sheetDialog.findViewById<TextView>(R.id.shareToQQ)?.setOnClickListener {
            share(SHARE_MEDIA.QQ)
        }

        sheetDialog.findViewById<TextView>(R.id.shareToWXCircle)?.setOnClickListener {
            share(SHARE_MEDIA.WEIXIN_CIRCLE)
        }
        sheetDialog.show()
    }


    private var width: Int = 0
    private var height: Int = 0


    private fun createDrawable(letter: String): Bitmap? {
        val imgTemp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(imgTemp)
        val paint = Paint() // 建立画笔
        paint.isDither = true
        paint.isFilterBitmap = true
        val src = Rect(0, 0, width, height)
        val dst = Rect(0, 0, width, height)
        canvas.drawBitmap(imgMarker, src, dst, paint)
        val textPaint = Paint(Paint.ANTI_ALIAS_FLAG
                or Paint.DEV_KERN_TEXT_FLAG)
        val textPaint1 = Paint(Paint.ANTI_ALIAS_FLAG
                or Paint.DEV_KERN_TEXT_FLAG)
        textPaint.textSize = 100.0f
        textPaint1.textSize = 60.0f
//        textPaint.typeface = Typeface.DEFAULT_BOLD // 采用默认的宽度
        textPaint.color = Color.WHITE
        textPaint1.color = Color.WHITE
        canvas.drawText(letter, (width / 2 - 150).toFloat(), (height / 2 + 50).toFloat(),
                textPaint)
        canvas.drawText("邀请码", (width / 2 - 90).toFloat(), (height / 2 - 100).toFloat(),
                textPaint1)
        canvas.save(Canvas.ALL_SAVE_FLAG)
        canvas.restore()
        return imgTemp
    }

    private lateinit var imgMarker: Bitmap
    private fun share(share_media: SHARE_MEDIA) {
        imgMarker = BitmapFactory.decodeResource(resources, R.drawable.bg_plan_pool)
        width = imgMarker.width
        height = imgMarker.height;
        val scale = width / 375
        val size = 125 * scale
        val codeBitmap = QrCodeUtil.createQRCodeWithLogo("http://download.odinlink.club", size, BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
        imgMarker = QrCodeUtil.compositeShareImage(getInviteCode()?.let { createDrawable(it) }, codeBitmap, intArrayOf((150 * scale), (530 * scale)))
        val umImage = UMImage(this, imgMarker)
        umImage.setThumb(UMImage(this, imgMarker))
        ShareAction(this)
                .setPlatform(share_media)
                .withMedia(umImage)
                .setCallback(object : UMShareListener {
                    override fun onResult(p0: SHARE_MEDIA?) {
                    }

                    override fun onCancel(p0: SHARE_MEDIA?) {
                    }

                    override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                    }

                    override fun onStart(p0: SHARE_MEDIA?) {
                    }
                })
                .share();
    }

    override fun createPresenter(): ProPlanPresenter {
        return ProPlanPresenter()
    }

    override fun createView(): IProPlanView {
        return this
    }

    override fun setRanks(ranks: List<Rank>?) {
        proPlanAdapter.setNewData(ranks)
    }
}